import config from "@/config";
import http from "@/utils/request";

export default {
	upload: {
		url: `${config.API_URL}/upload/files/part`,
		finishUrl: `${config.API_URL}/upload/files/part/finish`,
		name: "上传",
		fileToBase64Async: function (file) {
			return new Promise((resolve) => {
				let reader = new FileReader();
				reader.readAsDataURL(file);
				reader.onloadend = () => {
					//e resolve(e.target.result);
					// 获取Base64数据
					const base64Data = reader.result.split(",")[1];
					// 将Base64数据传递给resolve()函数
					resolve(base64Data);
					reader.onloadend = null;
					reader.onload = null;
					reader.abort();
					reader.result = null;
				};
			});
		},
		uploadFileChunk: async function (
			config,
			file,
			fileName,
			size,
			chunks,
			number,
			bufferChunkSize,
			uploadId,
			path
		) {
			if (number < chunks.length) {
				let chunk = chunks[number];
				let filePartName = fileName;
				var base64Data = await this.fileToBase64Async(
					file.slice(chunk.start, chunk.end)
				);
				let parameters = {
					partNumber: number + 1,
					chunks: chunks.length,
					size: bufferChunkSize,
					start: chunk.start,
					end: chunk.end,
					total: size,
					base64: base64Data,
					fileName: filePartName,
					uploadId: uploadId,
					path: path,
					calc:
						chunk.end > size
							? size - chunk.start
							: chunk.end - chunk.start,
				};
				let response = await http.post(this.url, parameters);
				parameters = {};
				chunk = [];
				base64Data = null;
				if (response.code == 200 && response.data != null) {
					uploadId = response.data.uploadId;
					path = response.data.path;
				}
				response = {};
				number = number + 1;
				return await this.uploadFileChunk(
					config,
					file,
					fileName,
					size,
					chunks,
					number,
					bufferChunkSize,
					uploadId,
					path
				);
			} else {
				let finishResponse = await http.post(
					this.finishUrl,
					{
						uploadId: uploadId,
						path: path,
					},
					config
				);

				return finishResponse;
			}
		},
		post: async function (file, config = {}) {
			let data = file.get("file");

			let fileChunks = [];
			let maxFileSizeMB = 5;
			let bufferChunkSize = maxFileSizeMB * 1024 * 1024;
			let fileStreamPos = 0;
			let endPos = bufferChunkSize;
			let size = data.size;
			while (fileStreamPos < size) {
				let fileChunkInfo = {
					// file:,
					start: fileStreamPos,
					end: endPos,
				};
				fileChunks.push(fileChunkInfo);
				fileStreamPos = endPos;
				endPos = fileStreamPos + bufferChunkSize;
			}

			return await this.uploadFileChunk(
				config,
				data,
				data.name,
				size,
				fileChunks,
				0,
				bufferChunkSize,
				"",
				""
			);
		},
	},
	upload2: {
		url: `${config.API_URL}/upload/upload`,
		name: "上传文件",
		post: async function (data) {
			let forms = new FormData();
			forms.append("file", data);
			return await http.post(this.url, forms);
		},
	},
	upload3: {
		url: `${config.API_URL}/upload/upload/file/fromForm`,
		name: "上传文件",
		post: async function (data) {
			let forms = new FormData();
			forms.append("file", data);
			return await http.post(this.url, forms);
		},
	},
	page: {
		url: `${config.API_URL}/upload/page/list`,
		name: "文件分页列表",
		get: async function (data) {
			return await http.post(this.url, data);
		},
	},
	remove: {
		url: `${config.API_URL}/upload/remove/`,
		name: "文件分页列表",
		post: async function (data) {
			return await http.post(this.url + data);
		},
	},
};
